/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.games.epkb.controller;

import it.polimi.games.epkb.controller.exceptions.IllegalOrphanException;
import it.polimi.games.epkb.controller.exceptions.NonexistentEntityException;
import it.polimi.games.epkb.controller.exceptions.PreexistingEntityException;
import it.polimi.games.epkb.entityClasses.UpdateKb;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import it.polimi.games.epkb.entityClasses.Actions;
import java.util.ArrayList;
import javax.transaction.UserTransaction;

/**
 *
 * @author plebani
 */
public class UpdateKbJpaController implements Serializable {

    public UpdateKbJpaController(EntityManagerFactory emf) {
        this.utx = null;
        this.emf = emf;
    }
    public UpdateKbJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(UpdateKb updateKb) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        Actions actionsOrphanCheck = updateKb.getActions();
        if (actionsOrphanCheck != null) {
            UpdateKb oldUpdateKbOfActions = actionsOrphanCheck.getUpdateKb();
            if (oldUpdateKbOfActions != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Actions " + actionsOrphanCheck + " already has an item of type UpdateKb whose actions column cannot be null. Please make another selection for the actions field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Actions actions = updateKb.getActions();
            if (actions != null) {
                actions = em.getReference(actions.getClass(), actions.getId());
                updateKb.setActions(actions);
            }
            em.persist(updateKb);
            if (actions != null) {
                actions.setUpdateKb(updateKb);
                actions = em.merge(actions);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findUpdateKb(updateKb.getId()) != null) {
                throw new PreexistingEntityException("UpdateKb " + updateKb + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(UpdateKb updateKb) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UpdateKb persistentUpdateKb = em.find(UpdateKb.class, updateKb.getId());
            Actions actionsOld = persistentUpdateKb.getActions();
            Actions actionsNew = updateKb.getActions();
            List<String> illegalOrphanMessages = null;
            if (actionsNew != null && !actionsNew.equals(actionsOld)) {
                UpdateKb oldUpdateKbOfActions = actionsNew.getUpdateKb();
                if (oldUpdateKbOfActions != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Actions " + actionsNew + " already has an item of type UpdateKb whose actions column cannot be null. Please make another selection for the actions field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (actionsNew != null) {
                actionsNew = em.getReference(actionsNew.getClass(), actionsNew.getId());
                updateKb.setActions(actionsNew);
            }
            updateKb = em.merge(updateKb);
            if (actionsOld != null && !actionsOld.equals(actionsNew)) {
                actionsOld.setUpdateKb(null);
                actionsOld = em.merge(actionsOld);
            }
            if (actionsNew != null && !actionsNew.equals(actionsOld)) {
                actionsNew.setUpdateKb(updateKb);
                actionsNew = em.merge(actionsNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = updateKb.getId();
                if (findUpdateKb(id) == null) {
                    throw new NonexistentEntityException("The updateKb with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            UpdateKb updateKb;
            try {
                updateKb = em.getReference(UpdateKb.class, id);
                updateKb.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The updateKb with id " + id + " no longer exists.", enfe);
            }
            Actions actions = updateKb.getActions();
            if (actions != null) {
                actions.setUpdateKb(null);
                actions = em.merge(actions);
            }
            em.remove(updateKb);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<UpdateKb> findUpdateKbEntities() {
        return findUpdateKbEntities(true, -1, -1);
    }

    public List<UpdateKb> findUpdateKbEntities(int maxResults, int firstResult) {
        return findUpdateKbEntities(false, maxResults, firstResult);
    }

    private List<UpdateKb> findUpdateKbEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(UpdateKb.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public UpdateKb findUpdateKb(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(UpdateKb.class, id);
        } finally {
            em.close();
        }
    }

    public int getUpdateKbCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<UpdateKb> rt = cq.from(UpdateKb.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
